<?php

namespace App\Http\Controllers\Estate;

use App\Http\Controllers\Controller;
use App\Models\IntegralGoods;
use App\Models\IntegralSet;
use App\Models\Store;
use APP\Models\StoresCate;
use App\Services\IntegralSetService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

/**
 * 积分设置
 * Class IntegralSetController
 * @package App\Http\Controllers\Estate
 */
class IntegralSetController extends Controller
{


    /**
     * 2021/3/15--11:12
     * @param Request $request
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:index
     * @effect:积分设置列表
     */
    public function index(Request $request)
    {
        $list = IntegralSetService::getSetList();
        return $this->success($list);
    }


    /**
     * 2021/3/15--11:12
     * @param Request $request
     * @param IntegralSet $set_model
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:store
     * @effect:添加积分设置
     */
    public function store(Request $request, IntegralSet $set_model)
    {

        $data = [
            'type' => $request->type ?? 1,                                        // 类型 1=>商家分类,2=>商家
            'belong_id' => $request->belong_id ?? '',                             // 属于某个商家分类或者是商家ID
            'scenes_type' => implode(',', $request->scenes_type) ?? '',   // 场景类型,1=>用户下单,2=>用户收货
            'state' => $request->state ?? '',                                    // 是否开启 1=>开启,2=>关闭
            'integral' => $request->integral ?? 0,
            'ratio' => json_encode($request->ratio) ?? '',
            'ratio_status' => $request->ratio_status ?? ''
        ];
        $set_model->create($data);
        return $this->success([], __('base.success'));

    }


    /**
     * 2021/3/15--11:13
     * @param IntegralSet $set_model
     * @param $id
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:show
     * @effect:展示积分设置
     */
    public function show(IntegralSet $set_model, $id)
    {
        $info = $set_model->find($id);
        $info['scenes_type'] = $info['scenes_type'];
        // 积分配置
        if (empty($info['ratio'])) {
            $info['ratio'] = [
                'integral' => 1,
                'ratio_price' => 1,
                'integral_upper_limit' => 1
            ];
        } else {
            $info['ratio'] = json_decode($info['ratio'], true);
        }
        return $this->success($info);
    }


    /**
     * 2021/3/15--11:13
     * @param Request $request
     * @param IntegralSet $set_model
     * @param $id
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:update
     * @effect:修改积分设置
     */
    public function update(Request $request, IntegralSet $set_model, $id)
    {


        $model = $set_model->find($id);
        // 类型
        if (isset($request->type) && !empty($request->type)) {
            $model->type = $request->type;
        }

        if (isset($request->belong_id) && !empty($request->belong_id)) {
            $model->belong_id = $request->belong_id;
        }

        if (isset($request->scenes_type) && !empty($request->scenes_type)) {
            //$model->scenes_type = implode(',', $request->scenes_type ?? []);
            $model->scenes_type = $request->scenes_type;
        }

//        if (isset($request->state) && !empty($request->state)) {

        $model->state = $request->state;
//        }

        if (isset($request->integral) && !empty($request->integral)) {
            $model->integral = $request->integral;
        }

        if (isset($request->ratio) && !empty($request->ratio)) {
            $model->ratio = json_encode($request->ratio);
        }
        $model->ratio_status = $request->ratio_status;
        $model->save();
        return $this->success([], __('base.success'));
    }


    /**
     * 2021/3/15--11:13
     * @param IntegralSet $set_model
     * @param $id
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:destroy
     * @effect:删除积分设置
     */
    public function destroy(IntegralSet $set_model, $id)
    {
        $idArray = array_filter(explode(',', $id), function ($item) {
            return is_numeric($item);
        });
        $set_model->destroy($idArray);
        return $this->success([], __('base.success'));
    }


    /**
     * 2021/3/15--11:31
     * @param Request $request
     * @param IntegralSet $model
     * @return array
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:integral_set_edit_state
     * @effect:修改积分设置启用状态
     */
    public function integral_set_edit_state(Request $request, IntegralSet $model)
    {
        $id = $request->post('id');
        $filed = $request->post('filed');
        $data = $model->where('id', $id)->first();
        $state = $data[$filed] == 1 ? 0 : 1;
        $data->$filed = $state;
        return $data->save() ? $this->success([], '修改成功!') : $this->error([], '修改失败!');
    }

    /**
     * 2021/3/15--11:13
     * @return array[]
     * @author:董宗澳
     * @email:2453611300
     * @wechat:DZA74941
     * @QQ:2453611300
     * @web:www.quzha.net
     * @method:scenesType
     * @effect:积分设置类型
     */
    public function scenesType()
    {

        return [
            ['label' => '用戶下单', 'value' => 1],
            ['label' => '用戶收货', 'value' => 2],
        ];

    }
}

